package org.springframework.prospring.ticket.service.impl;

import org.springframework.prospring.ticket.service.RegionManager;
import org.springframework.prospring.ticket.dao.RegionDAO;
import org.springframework.prospring.ticket.domain.location.Region;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Collection;

public class RegionManagerImpl implements RegionManager {
    private static Log log = LogFactory.getLog(RegionManagerImpl.class);
    private RegionDAO dao;

    public RegionManagerImpl() {
        super();
        if (RegionManagerImpl.log.isDebugEnabled()) {
            RegionManagerImpl.log.debug("in RegionManagerImpl Constructor");
        }
    }

    public void setRegionDAO(RegionDAO dao) {
        if (RegionManagerImpl.log.isDebugEnabled()) {
            RegionManagerImpl.log.debug("setting RegionDAO: " + dao);
        }

        this.dao = dao;
    }


    public Collection<Region> findAllRegions(){
        if (RegionManagerImpl.log.isDebugEnabled()) {
            RegionManagerImpl.log.debug("finding all Regions ");
        }
        Collection<Region> regions = dao.findAllRegions();

        if (regions == null) {
            RegionManagerImpl.log.warn("Not found any Regions in database.");
        }

        return regions;
    }


    public Region saveRegion(Region region) throws Exception {
        if (RegionManagerImpl.log.isDebugEnabled()) {
            RegionManagerImpl.log.debug("saving Region: " + region);
        }

        dao.saveRegion(region);

        return region;
    }

    public Region findByBusinessPK(String regionBusinessPK){
        if (RegionManagerImpl.log.isDebugEnabled()) {
            RegionManagerImpl.log.debug("finding region by BusinessPK: " + regionBusinessPK);
        }

        Region region = dao.findByBusinessPK(regionBusinessPK);

        return region;
    }

    public Region findById(Long regionId) {
        if (RegionManagerImpl.log.isDebugEnabled()) {
            RegionManagerImpl.log.debug("finding region by id: " + regionId);
        }

        Region region = dao.findById(regionId);

        return region;                
    }


    public void removeRegion(Long regionId) {
        dao.removeRegion(regionId);
    }

}
